package com.liang.practice.server.context;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 用户登录信息上下文
 */
public class LoginContextHolder {
    private static final InheritableThreadLocal<Map<String, Object>> itl = new InheritableThreadLocal<>();

    /**
     * 保存用户信息到ThreadLocal
     */
    public static void set(String key, Object value) {
        ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
        map.put(key, value);
        itl.set(map);
    }

    /**
     * 获取当前用户信息
     */
    public static String getLoginId(String key) {
        return (String) itl.get().get(key);
    }

    /**
     * 移除用户登录信息
     */
    public static void remove() {
        itl.remove();
    }
}
